//
//  SearchWorkViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/13.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "SearchWorkViewController.h"
#import "WorkerView.h"
#import "Base.h"
#import "ReadyDiSignle.h"

#import "UIImage+imagedown.h"
#import "AddViewController.h"
@interface SearchWorkViewController ()<UITextFieldDelegate>
{
    WorkerView *_itemView;
    UITextField *_textfield;
    UIButton *_button;
    
    UIView *_chooseView;
}
@end

@implementation SearchWorkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.title=@"查询员工";
    self.view.backgroundColor=COLOR;
    
    [self addviews];
    
    // Do any additional setup after loading the view.
}
-(void)addviews
{
    _textfield=[[UITextField alloc] init];
    _textfield.backgroundColor=[UIColor whiteColor];
    _textfield.frame=CGRectMake(0, 64, WIDTH-40, 40);
    _textfield.delegate=self;
    _textfield.placeholder=@"请输入员工账号";
    [self.view addSubview:_textfield];
    _textfield.textColor=[UIColor grayColor];
    _button=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH-40, 64, 40, 40)];
        //[_button setTitle:@"查询" forState:UIControlStateNormal];
    [_button setImage:[UIImage imageNamed:@"searchios.png"] forState:UIControlStateNormal];
    
    _button.backgroundColor=[UIColor whiteColor];
//    _button.layer.borderWidth=1.0;
    [self.view addSubview:_button];
    
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonAction
{
    [ReadyDiSignle signle].oidString=_textfield.text;
    
    [[ReadyDiSignle signle] chaxunWorker];
    
    
    
   
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"workerItem" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"workerItem"]) 
        {
        [self shuju];
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"workerItem"];
       }
    else
        {
        
        }
}
-(void)shuju
{
    UserModel *user=[ReadyDiSignle signle].workerItem[0];
    
    [_itemView removeFromSuperview];
    _itemView=[[WorkerView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(_textfield.frame)+10, WIDTH-10, (WIDTH-10)/4+45)];
    _itemView.backgroundColor=[UIColor whiteColor];
    _itemView.oidLaber.text=user.account;
    if (user.photo==nil)
        {
        _itemView.photoImage.image=[UIImage imageNamed:@"default_userhead.jpg"];
        }
    else
        {
        _itemView.photoImage.image=[UIImage imageDownWithurlstring:user.photo imagebolock:^(UIImage *img) {
            _itemView.photoImage.image=img;
        }];
        }
        //        _itemView.photoImage.backgroundColor=[UIColor redColor];
    [_itemView.logoButton setImage:[UIImage imageNamed:@"shitios.png"] forState:UIControlStateNormal];
    NSString *s=[self setrole:user.role.integerValue];
    if (user.type.intValue==1) {
        _itemView.typeLaber.text=@"全职";
    }
    if (user.type.intValue==2) {
        _itemView.typeLaber.text=@"兼职";
    }
    if (user.type.intValue==3) {
        _itemView.typeLaber.text=@"实习";
    }
    if (user.type.intValue==4) {
        _itemView.typeLaber.text=@"试用";
    }
    if(user.free.intValue==0)
        {
        _itemView.offLaber.text=@"上班";
        }
    else{
        _itemView.offLaber.text=@"下班";
    }

    _itemView.roleLaber.text=[NSString stringWithFormat:@"%@*%@*%@",user.mktName,user.dptName,s];
    _itemView.nameLaber.text=user.name;
    _itemView.phoneLaber.text=user.tel;
   
    [_itemView.logoButton addTarget:self action:@selector(shitAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_itemView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)shitAction
{
   UserModel *user=[ReadyDiSignle signle].workerItem[0];
         
    
        [_chooseView removeFromSuperview];
        _chooseView=[[UIView alloc] initWithFrame:CGRectMake(WIDTH/2-60, HEIGHT/2-100, 120, 200)];
        _chooseView.backgroundColor=[UIColor colorWithRed:217/256.0 green:217/256.0 blue:217/256.0 alpha:1];
      _chooseView.layer.cornerRadius=4;
        NSArray *array;
        if (user.enable.intValue==0)
            {
            array=@[@"修改信息",@"重置密码",@"解冻账号",@"员工离职",@"取消"];
            }
        else
            {
            array=@[@"修改信息",@"重置密码",@"冻结账号",@"员工离职",@"取消"];
            
            }
        for (int i=0; i<5; i++)
            {
            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0+40*i, 120, 40)];
            [button setTitle:array[i] forState:UIControlStateNormal];
            [_chooseView addSubview:button];
            [button addTarget:self action:@selector(roleAction:) forControlEvents:UIControlEventTouchUpInside];
            }
        
        [self.view addSubview:_chooseView];
        
 
}
-(void)roleAction:(UIButton *)sender
{
    UserModel *user=[ReadyDiSignle signle].workerItem[0];

    [ReadyDiSignle signle].oidString=user._id;
    if ([sender.titleLabel.text isEqualToString:@"修改信息"]) 
        {
        [_chooseView removeFromSuperview];
        
        AddViewController *add=[[AddViewController alloc] init];
        add.usermodel=user;
        [self.navigationController pushViewController:add animated:YES];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"重置密码"]) 
        {
        [[ReadyDiSignle signle] setPassWord:user.account];
        
        [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"冻结账号"]) 
        {
        
        [[ReadyDiSignle signle] dongjieWorking];
        [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"解冻账号"]) 
        {
        
        [[ReadyDiSignle signle] jiedongWorking];
        [_chooseView removeFromSuperview];
        }
    
    else  if ([sender.titleLabel.text isEqualToString:@"员工离职"]) 
        {
        
        [[ReadyDiSignle signle] lizhiWorking];
        [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"取消"]) 
        {
        [_chooseView removeFromSuperview];
        }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
